  var maxProfit = function (prices) {
    if (prices.length === 0) return 0
    let minPirce = prices[0],
      maxProfit = 0;
    for (let index = 0; index < prices.length; index++) {
      if (prices[index] < minPirce) {
        minPirce = prices[index]
      } else if ((prices[index] - minPirce) > maxProfit) {
        maxProfit = prices[index] - minPirce
      }
    }
    return maxProfit
  }

console.log(maxProfit([7,1,5,3,6,4]));